//
//  VentstacksScene.m
//  projecthz
//
//  Created by YDJH on 2017/5/3.
//  Copyright © 2017年 tali. All rights reserved.
//

#import "VentstacksScene.h"
#import "HZRoom.h"
#import "Masonry.h"
#import "HZFurniture.h"
#import "HZInsideWall.h"
#import "HZFurniture_Header.h"

@interface VentstacksScene ()

@property (nonatomic,copy) VentstacksSceneDownback callback;

@property (nonatomic,strong)HZRoom *nextRoom;   //下一个
@property (nonatomic,strong)HZRoom *currentRoom;  // 当前
@property (nonatomic,strong)HZRoom *lastRoom;       //上一个

@property (nonatomic,strong)UIButton *leftArrow;
@property (nonatomic,strong)UIButton *rightArrow;
@property (nonatomic,strong)UIButton *downArrow;
@property (nonatomic,strong)UIImageView *ventstacksImage;

@property (nonatomic,strong)NSMutableArray *dataArray;

@property (nonatomic, assign) NSInteger currentIndex;

@end

@implementation VentstacksScene

#pragma mark - Getter
- (UIImageView *)ventstacksImage {
    if (!_ventstacksImage) {
        _ventstacksImage = [[UIImageView alloc] init];
        _ventstacksImage.userInteractionEnabled = YES;
        _ventstacksImage.backgroundColor = [UIColor whiteColor];
    }
    return _ventstacksImage;
}

- (UIButton *)leftArrow {
    if (!_leftArrow) {
        _leftArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [_leftArrow setImage:[UIImage imageNamed:@"leftArrow"] forState:UIControlStateNormal];
        [_leftArrow addTarget:self action:@selector(leftArrowAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _leftArrow;
}

- (UIButton *)rightArrow {
    if (!_rightArrow) {
        _rightArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [_rightArrow setImage:[UIImage imageNamed:@"rightArrow"] forState:UIControlStateNormal];
        [_rightArrow addTarget:self action:@selector(rightArrowAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _rightArrow;
}

- (UIButton *)downArrow {
    if (!_downArrow) {
        _downArrow = [UIButton buttonWithType:UIButtonTypeCustom];
        [_downArrow setImage:[UIImage imageNamed:@"downArrow"] forState:UIControlStateNormal];
        [_downArrow addTarget:self action:@selector(downArrowAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _downArrow;
}

#pragma mark - Init
- (instancetype)initWithFrame:(CGRect)frame callback:(VentstacksSceneDownback)callback
{
    self = [super initWithFrame:frame];
    if (self) {
        self.callback = callback;
        [self initUI];
    }
    return self;
}

- (void)initUI {
    self.backgroundColor = [UIColor blackColor];
    self.userInteractionEnabled = YES;
    [self addSubview:self.ventstacksImage];
    [self addSubview:self.downArrow];
    [self addSubview:self.leftArrow];
    [self addSubview:self.rightArrow];
    
    [self.ventstacksImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 60));
        make.top.equalTo(self.mas_top).offset(20);
    }];
    
    [self.leftArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.centerY.equalTo(self.ventstacksImage.mas_centerY).offset(0);
    }];
    [self.rightArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(0);
        make.size.mas_equalTo(CGSizeMake(60, 30));
        make.centerY.equalTo(self.ventstacksImage.mas_centerY).offset(0);
    }];
    [self.downArrow mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.ventstacksImage.mas_bottom).offset(0);
        make.size.mas_equalTo(CGSizeMake(30, 60));
        make.right.equalTo(self.rightArrow.mas_left).offset(0);
    }];
    
}

#pragma mark - Public Method

- (void)configureViewWithCurrentRoomIndex:(NSIndexPath *)index andRoomArray:(NSMutableArray <HZRoom *>*)array {
    self.currentIndex  = index.row;
    self.dataArray = array;
    [self updateUI];
}

#pragma mark - Private Method
- (void)updateUI {
    
    self.leftArrow.hidden = YES;
    self.rightArrow.hidden = YES;
    self.downArrow.hidden = YES;
    
    if (self.currentIndex-1 >= 0) {
      self.lastRoom = self.dataArray[self.currentIndex-1];
    }else {
        self.lastRoom = nil;
    }
    
    if (self.currentIndex+1 <= self.dataArray.count-1) {
        self.nextRoom = self.dataArray[self.currentIndex+1];
    }else {
        self.nextRoom = nil;
    }
    
    self.currentRoom = self.dataArray[self.currentIndex];
    
    if (self.lastRoom) {
        self.leftArrow.hidden = NO;
    }else {
        self.leftArrow.hidden = YES;
    }
    
    if (self.nextRoom) {
        self.rightArrow.hidden = NO;
    }else {
        self.rightArrow.hidden = YES;
    }
    
    if (self.currentRoom) {
        for (HZFurniture *furniture in self.currentRoom.insidewall1.furnitureArr) {
            if ([furniture.furnitureType isEqualToString:VentStacks] && ![furniture.furnitureState isEqualToString:destory_Type]) {
                self.downArrow.hidden = NO;
            }
        }
    }
}

#pragma mark - Action
- (void)downArrowAction:(UIButton *)btn {
    
    self.callback(VentStacksDirection_down,self.currentRoom);
}

- (void)leftArrowAction:(UIButton *)btn {
    
    self.currentRoom.roomState = RoomState_Came;
    self.currentIndex = (self.currentIndex-1 >= 0) ? self.currentIndex-1 : 0;
    [self updateUI];
    self.callback(VentStacksDirection_left,self.currentRoom);
    self.currentRoom.roomState = RoomState_Coming;
}

- (void)rightArrowAction:(UIButton *)btn {
    
    self.currentRoom.roomState = RoomState_Came;
    self.currentIndex = (self.currentIndex+1 <= self.dataArray.count-1) ? self.currentIndex+1 : self.dataArray.count-1;
    [self updateUI];
    self.callback(VentStacksDirection_right,self.currentRoom);
    self.currentRoom.roomState = RoomState_Coming;

}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
